package com.yulongtian.interviewWorks.day20220826;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/robot-in-a-grid-lcci
 *
 * @author YulongTian
 * @date 2022/8/26 - 15:26
 */
public class Test06 {
    public static void main(String[] args) {
        int[][] obstacleGrid = {{0, 1}, {1, 0}};
        System.out.println(pathWithObstacles(obstacleGrid));
    }

    public static List<List<Integer>> pathWithObstacles(int[][] obstacleGrid) {
        //1 障碍物 跳过  2 右边可以通过  5下边可以通过  7 下边和右边均可通过

        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        List<List<Integer>> res = new ArrayList<>();
        if (m == 1 && n == 1 && obstacleGrid[0][0] == 0) {
            List<Integer> temp = new ArrayList<>();
            temp.add(0);
            temp.add(0);
            res.add(temp);
            return res;
        }
        if (m == 1 && n == 1 || obstacleGrid[0][0] == 1) {
            return res;
        }
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                if (i == m - 1 && j == n - 1) {
                    if (obstacleGrid[i][j] == 1) {
                        return res;
                    }
                    obstacleGrid[i][j] = 2;
                    continue;
                }
                if (obstacleGrid[i][j] == 1) {
                    continue;
                }
                if (i + 1 >= m) {
                    if (obstacleGrid[i][j + 1] != 1) {
                        obstacleGrid[i][j] = 2;
                    } else {
                        obstacleGrid[i][j] = 1;
                    }
                } else if (i + 1 < m && j + 1 >= n) {
                    if (obstacleGrid[i + 1][j] != 1) {
                        obstacleGrid[i][j] = 5;
                    } else {
                        obstacleGrid[i][j] = 1;
                    }
                } else {
                    if (obstacleGrid[i + 1][j] == 1 && obstacleGrid[i][j + 1] == 1) {
                        obstacleGrid[i][j] = 1;
                        continue;
                    }
                    if (obstacleGrid[i + 1][j] != 1 && obstacleGrid[i][j + 1] != 1) {
                        obstacleGrid[i][j] = 7;
                        continue;
                    }
                    if (obstacleGrid[i + 1][j] != 1) {
                        obstacleGrid[i][j] = 5;
                        continue;
                    }
                    if (obstacleGrid[i][j + 1] != 1) {
                        obstacleGrid[i][j] = 2;
                    }
                }


            }
        }
        if (obstacleGrid[0][0] == 1) {
            return res;
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (obstacleGrid[i][j] == 7 || obstacleGrid[i][j] == 2) {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(i);
                    temp.add(j);
                    res.add(temp);
                    continue;
                }
                if (obstacleGrid[i][j] == 5) {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(i);
                    temp.add(j);
                    res.add(temp);
                    i++;
                    j--;
                }
            }
        }
        return res;
    }

}
